package com.dudy.authdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.dudy.authdemo.model.Person;
import com.dudy.authdemo.service.PersonService;
import com.dudy.authdemo.vo.PersonVO;
import com.dudy.common.base.CommonResponse;
import com.dudy.common.base.ResultCode;
import com.dudy.common.util.DingTalkUtil;
import com.dudy.common.util.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: nacos_test
 * @author: dudy
 * @create: 2020-12-21 22:12
 * @description:
 **/
@RestController
public class LoginController {

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private PersonService personService;

    @PostMapping("/login")
    public CommonResponse<String> login(PersonVO personVO) {
        return ResponseUtils.getResponse();
    }

    @PostMapping("/registry")
    public CommonResponse<String> registry(@RequestBody PersonVO personVO) {
        if (personVO == null) {
            return ResponseUtils.getResponse(ResultCode.REQUEST_PARAM_NULL);
        }
        Person person = new Person();
        BeanUtils.copyProperties(personVO, person);
        try {
            int insert = personService.insert(person);
            if (insert == 1) {
                return ResponseUtils.getResponse(ResultCode.SUCCESS);
            } else {
                return ResponseUtils.getResponse(ResultCode.FAIL.getCode(), "注册失败，请重试");
            }
        } catch (Exception e) {
            log.error("系统异常",e);
            DingTalkUtil.pushError("auth", this.getClass().getName(), "registry", JSONObject.toJSONString(personVO), e);
            return ResponseUtils.getResponse(ResultCode.FAIL);
        }
    }
}
